<template>
  <div class="timing">
    <el-row style="margin-bottom:10px;">
      <el-col :span="24">
        <el-form :inline="true" :model="formInline" class="demo-form-inline queryFrom">
          <el-form-item label="特效名称">
            <el-input v-model="formInline.effectName" clearable placeholder="" />
          </el-form-item>
          <el-form-item>
            <el-button plain @click="getUserList"><Icon type="ios-search" />查询</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <!-- 按钮 -->
    <el-row style="margin-bottom:10px;">
      <el-col :span="24">
        <el-button type="primary" @click="addUser('add')"><Icon type="md-add" />新 增</el-button>
      </el-col>
    </el-row>
    <!-- 表格 -->
    <testhttb @deleteSelectData="deleteSelectData" ref="httbRef" :table-col="tabUserList" :table-data="tableUserData" @refresh="getUserList" />
    <!-- 分页 -->
    <div style="text-align:right;margin-top:10px;">
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="dialogTableVisible">
      <el-form v-if="dialogTableVisible" ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm autoClass">
        <el-form-item label="特效名称" prop="effectName">
          <el-input v-model="ruleForm['effectName']" clearable placeholder="" />
        </el-form-item>
        <el-form-item label="特效类型" prop="effectType">
          <el-select v-model="ruleForm['effectType']" placeholder="请选择">
            <el-option
              v-for="(item,index) in effectListOptions"
              :key="index"
              :label="item.codeType"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="特效Url" prop="effectUrl">
          <el-input v-model="ruleForm['effectUrl']" clearable placeholder="" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="ruleForm['remark']" />
        </el-form-item>
        <el-form-item label="特效照片" prop="effectImg">
          <el-upload
            class="upload-demo"
            style="position:relative"
            :action="httpUrl+'/ht-user/user/upload'"
            :headers="headers"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            :on-success="onSuccess"
            :before-upload="beforeAvatarUpload"
            multiple
            :limit="1"
            :on-exceed="handleExceed"
            :file-list="fileList"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过2M</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="ruleForm['state']">
            <el-radio label="0">正常</el-radio>
            <el-radio label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">{{ btnStr }}</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
import { effectInsert, effectPageList, effectUpdate,effectRemoves,codeManagelistByCodeMain } from '@/api/home_/experiment'
import experiment from '@/mixins/experiment'
export default {
  components: {
  },
  mixins: [experiment],
  data() {
    return {
      httpUrl: process.env.VUE_APP_BASE_API,
      effectListOptions:[],
      headers: {
        Authorization: 'bearer ' + getToken()
      },
      fileList: [],
      ruleForm: {
        'effectImg': '',
        'effectName': '',
        'effectType': '',
        'effectUrl': '',
        'remark': '',
        'state': ''
      },
      title: '新增',
      rules: {},
      dialogTableVisible: false,
      depList: ['水流', '水压'],
      formInline: {
        effectName: '',
        effectType: ''
      },
      tableUserData: [],
      tabUserList: [
        {
          key: 'effectName',
          title: '特效名称'
        },
        {
          key: 'effectType',
          title: '特效类型'
        },
        {
          key: 'effectImg',
          title: '特效图片'
        },
        {
          key: 'effectUrl',
          title: '特效地址'
        },
        {
          key: 'remark',
          title: '备注'
        }, {
          key: 'status',
          title: '状态'
        }
      ],
      total: 0,
      size: 10,
      page: 1
    }
  },
  created() {
    codeManagelistByCodeMain('effect_type').then(res=>{
      this.effectListOptions = res.data
    })
  },
  mounted() {
    this.$nextTick(() => {
      this.getUserList()
    })
  },
  methods: {
    deleteSelectData(v) {
      effectRemoves({ ids: [v.effectId] }).then(res => {
        if (res.code == 200) {
          this.getUserList()
        } else {
          this.$notify.error({
            title: '错误',
            message: res.msg
          })
        }
      })
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      // if (!isJPG) {
      //   this.$message.error('上传头像图片只能是 JPG 格式!');
      // }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      // return isJPG && isLt2M;
      return isLt2M
    },
    onSuccess(v) {
      this.ruleForm.effectImg = v.data
    },
    handleRemove(file, fileList) {
    },
    handlePreview(file) {
    },
    handleExceed(files, fileList) {
      // this.$message.warning(`当前限制选择1个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
    },
    beforeRemove(file, fileList) {
      // return this.$confirm(`确定移除 ${file.name}？`)
    },
    commitFromData() {
      if (this.ruleForm.hasOwnProperty('id')) {
        effectUpdate(this.ruleForm).then(res => {
          if (res.code == 200) {
            this.getUserList()
          } else {
            this.errorDialog(res.msg)
          }
        })
      } else {
        effectInsert(this.ruleForm).then(res => {
          if (res.code == 200) {
            this.getUserList()
          } else {
            this.errorDialog(res.msg)
          }
        })
      }
    },
    handleSizeChange(v) {
      this.page = 1
      this.size = v
      this.getUserList()
    },
    handleCurrentChange(v) {
      this.page = v
      this.getUserList()
    },
    // 获取用户列表
    getUserList() {
      this.dialogTableVisible = false
      effectPageList({
        data: {
          'effectName': this.formInline.effectName,
          'effectType': this.formInline.effectType
        },
        'current': this.page,
        'size': this.size
      }).then(res => {
        this.tableUserData = res.data.records
        this.total = res.data.total
      })
    }
  }
}
</script>

  <style lang="scss" scoped>
  .el-select{
    width:100%;
  }
    .timing{
      background:#fff;
      padding:20px;
    }
    .autoClass{
    display:flex;
    flex-direction:row;
    flex-wrap:wrap;
    .el-form-item{
        width:50%;
    }
}
  </style>
